package com.qf.mybatisplus.utils.excel;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelWriter;
import com.qf.mybatisplus.bean.entity.User;
import com.qf.mybatisplus.bean.entity.VHdcallspecail;
import com.qf.mybatisplus.bean.entity.VJhmx;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ExcelUtil {

    public static void exportExcel(List dataList,Map headMap,OutputStream out)  {
        //1. 创建一个Excel写出工具Writer
        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter(true);
        //2. 设置别名
        Iterator headIt = headMap.keySet().iterator();
        while (headIt.hasNext()){
            String key = (String) headIt.next();
            String value = (String) headMap.get(key);
            writer.addHeaderAlias(key,value);
        }

        //3. 先输出一行标题(参数1是跨列数, 从0开始. 参数2 是标题字符串)
//        writer.merge(3,"标题");
        //4. 模拟list集合数据
//        List<UserDemo> users = getUsers();
        //5. 将users数据输出到excel文件中.
        writer.write(dataList,true);// true表示并输出标题。
        //6. 将填充数据后的excel文件保存.
        writer.flush(out);
    }


    /**
    * @Description web浏览器访问时在浏览器下载此表格
    * @Param [dataList 数据列表, headMap 表头, title 文件名, response]
    * @Author 12136
    * @Date 16:34 2021/11/30
    * @return void
    **/
    public static void downloadExcelFile(List dataList,Map headMap,String title, HttpServletResponse response) {
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
//            ExportExcelUtil.exportMessageExcelFile(title, headers, dataList, os, "yyyy-MM-dd");
            ExcelUtil.exportExcel(dataList,headMap,os);

            byte[] content = os.toByteArray();
            InputStream is = new ByteArrayInputStream(content);
            // 设置response参数，可以打开下载页面
            response.reset();

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String((title+".xlsx").getBytes("utf-8"), "ISO8859-1"));
            response.setContentLength(content.length);
            response.addHeader("Access-Control-Allow-Origin", "*");
            ServletOutputStream outputStream = response.getOutputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(outputStream);
            byte[] buff = new byte[8192];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);

            }
            bis.close();
            bos.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static List<VJhmx> importExcel(File excelFile,Map headMap){
        //1. 创建一个Excel读取工具reader
        ExcelReader reader = cn.hutool.poi.excel.ExcelUtil.getReader(excelFile);

        //2. 设置读取的数据的别名和封装的实体的属性对应关系.
        Iterator headIt = headMap.keySet().iterator();
        while (headIt.hasNext()){
            String key = (String) headIt.next();
            String value = (String) headMap.get(key);
            reader.addHeaderAlias(key,value);
        }
    /*
        reader读取excel文件数据
        参数说明
            headerRowIndex: 映射的数据标题在第几行, 从0开始算.
            startRowIndex: 实际读取的数据从第几行开始, 从0开始算.
            class: 读取的数据封装成什么类型的对象
     */
        List<VJhmx> VJhmxs = reader.read(0, 1, VJhmx.class);

        return VJhmxs;
    }


    public static List<VHdcallspecail> importExcelHt(File excelFile, Map headMap){
        //1. 创建一个Excel读取工具reader
        ExcelReader reader = cn.hutool.poi.excel.ExcelUtil.getReader(excelFile);

        //2. 设置读取的数据的别名和封装的实体的属性对应关系.
        Iterator headIt = headMap.keySet().iterator();
        while (headIt.hasNext()){
            String key = (String) headIt.next();
            String value = (String) headMap.get(key);
            reader.addHeaderAlias(key,value);
        }
    /*
        reader读取excel文件数据
        参数说明
            headerRowIndex: 映射的数据标题在第几行, 从0开始算.
            startRowIndex: 实际读取的数据从第几行开始, 从0开始算.
            class: 读取的数据封装成什么类型的对象
     */
        List<VHdcallspecail> vHdcallspecailList= reader.read(0, 1, VHdcallspecail.class);

        return vHdcallspecailList;
    }
}
